package com.bestbuy.controller;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.bestbuy.pojo.Account;
import com.bestbuy.pojo.Catalog;
import com.bestbuy.pojo.Product;
import com.bestbuy.service.AccountService;
import com.bestbuy.service.CatalogService;
import com.bestbuy.service.ProductService;

@Controller
public class SignupController
{
	@Autowired
	private AccountService accountService;
	@Autowired
	private CatalogService catalogService;
	@Autowired
	private ProductService productService;

	@RequestMapping(value = { "/signup.do" })
	public String RegisterAccount(Model model) 
	{
		model.addAttribute("Page", "AccountSignup");
		return "Main";
	}

	@RequestMapping(value = { "/signup.do" }, method = RequestMethod.POST)
	public String RegisterAccount
	(
			@RequestParam(value = "username") String username,
			@RequestParam(value = "password") String password,
			@RequestParam(value = "repassword") String repassword,
			HttpSession session, Model model) 
	{
		if (username.equals("") == false && password.equals("") == false) {
			if (password.equals(repassword) == true) 
			{
				// dang ki
				Account account = accountService.RegisterAccount(username, repassword);
				if (account != null) // dang ki thanh cong
				{
					
					//List<Catalog> listCatalog = catalogService.getListCatalog();
					List<Catalog> listCatalog = catalogService.getListCatalogByDeleted("0");
					model.addAttribute("catalogs", listCatalog);
					//List<Product> listProduct = productService.Search("", "0", "1");
					List<Product> listProduct = productService.Search("", "0", "1", "0");
					model.addAttribute("products", listProduct);
					// dua seesion wa
					session.setAttribute("username", account.getUsername());
					model.addAttribute("isIndex","SlideShow");
					
					model.addAttribute("Page", "Index");
					return "Main";
				} 
				else 
				{
					// thong bao loi
					String error = "Username or Password is adready!";
					// tra ve trang Signup
					model.addAttribute("error", error);
					model.addAttribute("Page", "AccountSignup");
					return "Main";
				}
			} 
			else 
			{
				// thong bao loi
				String error = "Retype password is not correct!";
				// tra ve trang Signup
				model.addAttribute("error", error);
				model.addAttribute("Page", "AccountSignup");
				return "Main";
			}
		} else {
			// thong bao loi
			String error = "Please Input Username or Password!";
			// tra ve trang Signup
			model.addAttribute("error",error);
			model.addAttribute("Page", "AccountSignup");
			return "Main";
		}
	}
}
